Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6! (Publisher: The Coriolis Group) Author(s): Peter G. Aitken ISBN: 1576102815 Publication Date: 08/01/98 Search this book:
Previous | Table of Contents | Next
## Chapter 9 Dynamic Data Exchange Dynamic Data Exchange allows your Visual Basic programs to exchange data with other Windows programs, as well as send them commands. Dynamic Data Exchange (DDE) is a feature of Windows that permits programs to send data back and forth. Within certain limitations, it also allows them to send commands to each other. It can be an extraordinarily useful technique, and in many situations is preferable to the other Windows method for interaction between programs: Object Linking and Embedding. (I introduced OLE in Chapter 8 and will cover it more fully in Chapter 10.) What exactly can DDE do? Once again, an example is the best way to explain. A while ago, I wrote a custom program in Visual Basic for the laboratory where I work; the program was designed to take measurements from digitized data that was collected from our experiments. The final destination for this data was an Excel spreadsheet that handles data manipulation and graphing. My first impulse was to have the Visual Basic program save the results in a text file, which could then be imported into Excel. After a few moments of thought about the capabilities of DDE, however, I came up with a much better way. The final program uses DDE to accomplish the following steps: 1. Start Excel and open a new worksheet. 2. Insert labels into specific cells in the worksheet to provide the date, data labels, and other experiment identifiers. 3. Poke analysis results numbers into specific worksheet cells as the analysis proceeds. 4. Place formulas in other worksheet cells to perform the needed calculations. 5. Save the worksheet file under a user-selected name. When running an analysis, all I need do is hit Alt+Tab or use the Windows taskbar to switch from the Visual Basic measurement program to Excel, where my data is ready for analysis and graphing. This is a real timesaver, and it represents the way that Windows programs are supposed to work together. ### DDE Basics Perhaps the most common way to copy data from one Windows application to another is to use the Clipboard. As handy as the Clipboard can be, however, it does not provide for automatic updating of the data. In other words, each time the data in the original changes, the copied data must be updated manually. DDE remedies this situation by providing the capability of automatically updating transferred data. Let’s look at a specific example. Suppose we are using a word processor to write a report, and we want to include some numbers from an Excel worksheet. We would start Excel, open the worksheet, select the numbers, and copy them to the Clipboard. Next, we would switch back to the word processor and paste the table into our document. Convenient—as long as we know the numbers will never change. In practice, this is often not the case. Sales figures change, forecasts are modified. How can we be sure that our report is up to date? With standard Clipboard copy-and-paste, we have to repeat the entire process of copying the numbers from the worksheet to the document. Worse yet, we may not even know when the numbers have changed if we are on a network where other people can modify the worksheet. DDE to the rescue! DDE allows us to establish a link between the worksheet and the document. This link gives us the ability to copy data and information about its source. We don’t see this information—it is stored internally—but it enables the program to update the copied information automatically when the source changes. We can establish any number of links between multiple applications. Furthermore, the linked data can be just about anything—numbers, text, or a graphical image, for example. With DDE, we need to enter data only once, and the DDE links automatically transfer it to any other place it is needed. The same is true of changes to existing data; manual updates are no longer necessary. The end result? We save time and decrease the potential for errors. TIP: Will OLE And ActiveX Replace DDE? You’ve probably heard about Object Linking and Embedding (OLE), which provides sophisticated methods for Windows applications to cooperate in creating compound documents. You’ve also heard about ActiveX, the hot new software component technology (covered in Chapter 8). Because OLE and ActiveX provide ways for programs to share data, you might wonder if they will eventually replace DDE. Should you ignore DDE and use OLE or ActiveX when your Visual Basic programs need to exchange data? I can’t predict the future, of course, but my bet is that the answer will be “no.” Why do I feel this way? OLE and ActiveX are certainly more powerful than DDE (you’ll see what I mean in later chapters). Unavoidable consequences of this power include a need for greater system resources, more difficulty in programming, and generally slower speed. OLE and ActiveX are always around when we need them; but when DDE will fill the bill, we’re better off using it. Why use a half-ton pickup truck to bring home a bag of groceries when your old Volkswagen can do the job, is easier to drive, and uses less gas? Considering Microsoft’s emphasis on backward compatibility, DDE support will probably never be removed from Windows. #### DDE Terminology When you use DDE to link two applications, one application is called the source and the other is the destination. As you can guess, the data to be exchanged originates in the source application and is transferred to one or more destination applications. The link is sometimes referred to as a conversation. It is always the destination application that initiates a DDE conversation. To initiate a conversation, the destination application must specify the name of the source application. Each Windows application that can act as a DDE source has a unique name for this purpose. Usually, this is the executable file name without the .EXE extension. The destination application must also specify the topic of the conversation. The topic identifies the unit of data being linked—a designated unit of information that is meaningful to the source. It often is the name of a data file. While the available DDE topics vary from application to application, one that is almost universally supported is the System topic. Using the System topic, a DDE destination can obtain the other topics that the source supports, as well as other information about the application, including the supported data formats. The actual data being transferred in the DDE conversation is termed the item. The item must be identified in a way that the source application can understand. Thus, a spreadsheet program, such as Lotus 1-2-3 or Microsoft Excel, understands the row and column addresses of spreadsheet cells, and a word processor, such as Microsoft Word, recognizes bookmark names. Here’s an example: For a destination application to establish a link to the contents of cell C12 in the Excel worksheet D:\REPORTS\MAY1998.XLS, the conversation will specify EXCEL as the application, D:\REPORTS\MAY1998.XLS as the topic, and R12C3 (row 12, column 3) as the item. Both the application and the topic remain constant throughout a DDE conversation. The conversation will terminate if either the source or the destination attempts to change the application or topic. In contrast, the item of a DDE conversation can change as many times as desired. For example, the same DDE conversation could refer to different spreadsheet cells at different times, as long as they are in the same file (a technique we’ll use later). Data is normally transferred from the source to the destination. As we’ll see later, however, data can travel in the other direction—from destination to source. At first glance, moving the data from destination to source may seem contradictory, but remember that the destination application is the one that initiates the DDE conversation, and it is usually—but not always—the destination that receives the transferred data. #### Updating DDE Links In addition to specifying the application, topic, and item, a DDE link specifies how the link is updated. Three methods are possible: • Automatic link— The destination is updated whenever the source data changes. • Manual link—The destination must specifically request each update from the source. • Notify link— The destination is automatically notified by the source whenever the data changes, but the data is updated only when the destination requests it. The type of link you will use depends, of course, on the specific situation.
Previous | Table of Contents | Next

| Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.